﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraEditors.Controls;
using System.Drawing;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Repository;

namespace ProgNetComponentsDx.Controls.Editors
{
    public class FontStyleEdit : ButtonEdit
    {
        static FontStyleEdit()
        {
            RepositoryItemFontStyleEdit.Register();
        }
        [Browsable(false)]
        public override string EditorTypeName { get { return "FontStyleEdit"; } }
        [Description("Gets an object containing properties, methods and events specific to the FontStyleEdit control."), Category(CategoryName.Properties), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemFontStyleEdit Properties { get { return base.Properties as RepositoryItemFontStyleEdit; } }
        protected override void CreateRepositoryItem()
        {
            if (fProperties == null ||
                (fProperties as RepositoryItemFontStyleEdit) == null)
            {
                this.fProperties = new RepositoryItemFontStyleEdit();
                Properties.CreateDefaultButton();
                (this.fProperties as RepositoryItemFontStyleEdit).SetOwner(this);
            }
        }
        public FontStyleEdit()
            : base()
        {
            Properties.CreateDefaultButton();
        }

        public override object EditValue
        {
            get
            {
                if (base.EditValue is Font)
                {
                    FontConverter f = new FontConverter();
                    return f.ConvertToString(base.EditValue);
                }
                return base.EditValue;
            }
            set
            {
                try
                {
                    FontConverter f = new FontConverter();
                    object obj = null;

                    if (value is Font)
                        obj = value;
                    else
                        obj = f.ConvertFrom(value);
                    if (obj != null && obj is Font)
                        base.EditValue = obj;
                    else
                        base.EditValue = obj;
                }
                catch
                {
                    base.EditValue = null;
                }
            }
        }
    }
    [UserRepositoryItem("Register")]
    public class RepositoryItemFontStyleEdit : RepositoryItemButtonEdit
    {
        public RepositoryItemFontStyleEdit()
        {
            base.TextEditStyle = TextEditStyles.DisableTextEditor;
            Buttons.CollectionChanged += new CollectionChangeEventHandler(Buttons_CollectionChanged);
        }

        void Buttons_CollectionChanged(object sender, CollectionChangeEventArgs e)
        {
            if (e.Action == CollectionChangeAction.Remove)
            {
                if ((e.Element as EditorButton).Tag.AsString() == "EDIT")
                {
                    Buttons.Add(e.Element as EditorButton);
                }
            }
        }
        static RepositoryItemFontStyleEdit()
        {
            Register();
        }
        public static void Register()
        {
            EditorRegistrationInfo.Default.Editors.Add(
                new EditorClassInfo("FontStyleEdit",
                                typeof(FontStyleEdit),
                                typeof(RepositoryItemFontStyleEdit),
                                typeof(DevExpress.XtraEditors.ViewInfo.ButtonEditViewInfo),
                                new DevExpress.XtraEditors.Drawing.ButtonEditPainter(),
                                true,
                                null));
        }


        private FontDialog openD;
        private FontDialog OpenDialog
        {
            get
            {
                if (openD == null)
                {
                    openD = new FontDialog();
                }
                return openD;
            }
        }
        
        [Browsable(false)]
        public override EditorButtonCollection Buttons
        {
            get
            {
                return base.Buttons;
            }
        }

        [Browsable(false)]
        public override TextEditStyles TextEditStyle
        {
            get
            {
                return TextEditStyles.DisableTextEditor;
            }
            set
            {
                //base.TextEditStyle = value;
            }
        }

        [Browsable(false)]
        public override string EditorTypeName { get { return "FontStyleEdit"; } }
        [Browsable(false)]
        public new FontStyleEdit OwnerEdit { get { return base.OwnerEdit as FontStyleEdit; } }

        public override void CreateDefaultButton()
        {
            Buttons.Clear();
            EditorButton btn = new EditorButton("EDIT", ButtonPredefines.Ellipsis);
            btn.IsDefaultButton = true;
            Buttons.Add(btn);
            this.ButtonClick -= new ButtonPressedEventHandler(RepositoryItemFontStyleEdit_ButtonClick);
            this.ButtonClick += new ButtonPressedEventHandler(RepositoryItemFontStyleEdit_ButtonClick);
        }
        public override BaseEdit CreateEditor()
        {
            return new FontStyleEdit();
        }

        public void SetOwner(BaseEdit edit)
        {
            base.SetOwnerEdit(edit);
        }
        void RepositoryItemFontStyleEdit_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            BaseEdit Edit = sender as BaseEdit;
            if (e.Button != null && !Edit.Properties.ReadOnly)
            {
                if (e.Button.Tag.AsString() == "EDIT")
                {
                    if (Edit != null)
                    {
                        if (Edit.EditValue != null)
                        {
                            try
                            {
                                FontConverter f = new FontConverter();

                                OpenDialog.Font = f.ConvertFrom(Edit.EditValue) as Font;
                            }
                            catch { }
                        }
                        if (OpenDialog.ShowDialog() == DialogResult.OK)
                        {
                            Edit.EditValue = OpenDialog.Font;
                        }
                    }
                }
            }

        }
    }
}
